package com.NULL.phony.filter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DB extends SQLiteOpenHelper {
	private final static String DB_NAME = "BlockedSMS";
	private final static String TABLE_NAME = "SMS";
	private final static String TABLE_CREATE = "CREATE TABLE SMS(ACCEPTEDTIME INTEGER, PHONE TEXT, MESSAGE TEXT)";
	private final static int DB_VERSION = 1;
	private final static int ACCEPTEDTIME_INDEX = 0;
	private final static int PHONE_INDEX = 1;
	private final static int MESSAGE_INDEX = 2;

	public DB(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	public void add(String phone, String message) {
		ContentValues values = new ContentValues();
		values.put("ACCEPTEDTIME", System.currentTimeMillis());
		values.put("PHONE", phone);
		values.put("MESSAGE", message);
		this.getWritableDatabase().insert(TABLE_NAME, null, values);
	}

	public void remove(long acceptedTime) {
		this.getWritableDatabase().delete(TABLE_NAME, "ACCEPTEDTIME=?",
				new String[] { acceptedTime + "" });
	}

	public Cursor query(long acceptedTime) {
		return this.getReadableDatabase().rawQuery(
				"SELECT * FROM " + TABLE_NAME + " WHERE ACCEPTEDTIME>?",
				new String[] { acceptedTime + "" });
	}

	public int getCount() {
		Cursor cursor = this.getReadableDatabase().rawQuery(
				"SELECT COUNT(*) FROM " + TABLE_NAME, null);
		cursor.moveToFirst();
		return cursor.getInt(0);
	}

	public long getAcceptedTime(Cursor cursor) {
		return cursor.getLong(ACCEPTEDTIME_INDEX);
	}

	public String getPhone(Cursor cursor) {
		return cursor.getString(PHONE_INDEX);
	}

	public String getMessage(Cursor cursor) {
		return cursor.getString(MESSAGE_INDEX);
	}
}
